package com.boilermaker.lotterysystem;


import com.boilermaker.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import com.boilermaker.lotterysystem.common.exception.ControllerException;
import com.boilermaker.lotterysystem.common.exception.ServiceException;
import com.boilermaker.lotterysystem.common.pojo.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    private final static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> serviceException(ServiceException e) {
        logger.error("ServiceException : ", e);

        return CommonResult.error(
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), e.getMsg());


    }

    @ExceptionHandler(value = ControllerException.class)
    public CommonResult<?> controllerException(ControllerException e) {
        logger.error("ControllerException : ", e);

        return CommonResult.error(
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), e.getMsg());


    }

    @ExceptionHandler(value = Exception.class)
    public CommonResult<?> exception(Exception e) {
        logger.error("Exception : ", e);

        return CommonResult.error(
                GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR);


    }
}
